package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.FileData;
import com.ruoyi.system.service.IFileDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件数据Controller
 *
 * @author cjie
 * @date 2021-06-13
 */
@Api(tags = " 文件数据")
@RestController
@RequestMapping("/fileData")
public class FileDataController extends BaseController {
    @Autowired
    private IFileDataService fileDataService;

    /**
     * 查询文件数据列表
     */
    @ApiOperation(value="查询文件数据列表", notes="查询文件数据列表")
    @PreAuthorize(hasPermi = "system:fileData:list")
    @GetMapping("/list")
    public TableDataInfo list(FileData fileData) {
        startPage();
        List<FileData> list = fileDataService.selectFileDataList(fileData);
        return getDataTable(list);
    }

    /**
     * 导出文件数据列表
     */
    @ApiOperation(value="导出文件数据列表", notes="导出文件数据列表")
    @PreAuthorize(hasPermi = "system:fileData:export")
    @Log(title = "文件数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FileData fileData) throws IOException {
        List<FileData> list = fileDataService.selectFileDataList(fileData);
        ExcelUtil<FileData> util = new ExcelUtil<FileData>(FileData.class);
        util.exportExcel(response, list, "文件数据数据");
    }
    /**
     * 获取文件数据详细信息
     */
    @ApiOperation(value="获取文件数据详细信息", notes="获取文件数据详细信息")
    @PreAuthorize(hasPermi = "system:fileData:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(fileDataService.selectFileDataById(id));
    }

    /**
     * 新增文件数据
     */
    @ApiOperation(value="新增文件数据", notes="新增文件数据")
    @PreAuthorize(hasPermi = "system:fileData:add")
    @Log(title = "文件数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FileData fileData) {
        return toAjax(fileDataService.insertFileData(fileData));
    }

    /**
     * 批量新增图片
     */
    @ApiOperation(value="批量新增图片", notes="批量新增图片")
    @PreAuthorize(hasPermi = "system:fileData:add")
    @Log(title = "批量新增图片", businessType = BusinessType.INSERT)
    @PostMapping("/addImages")
    public AjaxResult addImages(@RequestBody FileData[] fileDatas) {
        int num=0;
        List errFiles=new ArrayList();
        for (FileData fileData : fileDatas){
            int result=fileDataService.insertFileData(fileData);
            if (result>0){
                num+=result;
            }else {
                errFiles.add(fileData);
            }
        }
        if (num==fileDatas.length){
            return AjaxResult.success("所有文件录入成功");
        }else {
            String errorMessage="有"+(fileDatas.length-num)+"个插入失败！";
            return AjaxResult.error(errorMessage,errFiles);
        }
    }

    /**
     * 修改文件数据
     */
    @ApiOperation(value="修改文件数据", notes="修改文件数据")
    @PreAuthorize(hasPermi = "system:fileData:edit")
    @Log(title = "文件数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FileData fileData) {
        return toAjax(fileDataService.updateFileData(fileData));
    }

    /**
     * 删除文件数据
     */
    @ApiOperation(value="删除文件数据", notes="删除文件数据")
    @ApiImplicitParam(name = "ids", value = "文件数据ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "system:fileData:remove")
    @Log(title = "文件数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fileDataService.deleteFileDataByIds(ids));
    }
}
